#!/usr/bin/env python
# -*- conding: utf-8 -*-

"""
@Time     : 2024/9/2 7:37
@Author   : liujingmao
@File     : 1.weaviate嵌入向量数据库示例.py
"""
import dotenv
import weaviate
from langchain_openai import OpenAIEmbeddings
from langchain_weaviate import WeaviateVectorStore
from weaviate.auth import AuthApiKey

dotenv.load_dotenv()

# 1.原始文本数据与元数据
texts = [
    "笨笨是一只很喜欢睡觉的猫咪",
    "我喜欢在夜晚听音乐，这让我感到放松。",
    "猫咪在窗台上打盹，看起来非常可爱。",
    "学习新技能是每个人都应该追求的目标。",
    "我最喜欢的食物是意大利面，尤其是番茄酱的那种。",
    "昨晚我做了一个奇怪的梦，梦见自己在太空飞行。",
    "我的手机突然关机了，让我有些焦虑。",
    "阅读是我每天都会做的事情，我觉得很充实。",
    "他们一起计划了一次周末的野餐，希望天气能好。",
    "我的狗喜欢追逐球，看起来非常开心。",
]
metadatas = [
    {"page": 1},
    {"page": 2},
    {"page": 3},
    {"page": 4},
    {"page": 5},
    {"page": 6, "account_id": 1},
    {"page": 7},
    {"page": 8},
    {"page": 9},
    {"page": 10},
]

# 2.创建连接客户端
# 本地连接
# client = weaviate.connect_to_local("192.168.2.120", "8080")
# 远程云连接
client = weaviate.connect_to_wcs(
    cluster_url="https://u5elrglqdgrd8ohzixxzq.c0.us-west3.gcp.weaviate.cloud",
    auth_credentials=AuthApiKey("1yebhV73vCkxE8xqfqA2Q6zoI7YzIl3A1RZP"),
)

embedding = OpenAIEmbeddings(model="text-embedding-3-small")

# 3.创建LangChain向量数据库实例
db = WeaviateVectorStore(
    client=client,
    index_name="Dataset",
    text_key="text",
    embedding=embedding,
)

# 4.添加数据
# ids = db.add_texts(texts, metadatas)
# print(ids)

# 5.执行相似性搜索
# filters = Filter.by_property("page").greater_or_equal(5)
# print(db.similarity_search_with_score("笨笨", filters=filters))
retriever = db.as_retriever()
print(retriever.invoke("笨笨"))

client.close()

"""
['350c536e-c072-4ed2-92f5-7ab2aa28ffd9', '021aebff-b007-46ac-b95c-b13712e21f5b', '42a13884-f559-430a-b688-f57794e4462b', '3036ffd9-043e-47d4-b7ce-12d2af173746', '36b8d7b8-e945-4076-bffb-194cea4190c9', '9acbeba5-6825-4693-8de8-3db8697e1fed', '368b3f11-ab35-4f2b-9952-2fdd7329eee9', '4838aea1-5e21-4180-965a-922dc58f63a9', 'ab8c0097-fe45-4edf-a3d1-d18ed270c801', 'af67d563-8c40-4cee-9079-e0823f7e43ff']
[(Document(metadata={'page': 10.0, 'account_id': None}, page_content='我的狗喜欢追逐球，看起来非常开心。'), 0.699999988079071), (Document(metadata={'page': 7.0, 'account_id': None}, page_content='我的手机突然关机了，让我有些焦虑。'), 0.4045487940311432), (Document(metadata={'page': 6.0, 'account_id': 1.0}, page_content='昨晚我做了一个奇怪的梦，梦见自己在太空飞行。'), 0.318904846906662), (Document(metadata={'page': 5.0, 'account_id': None}, page_content='我最喜欢的食物是意大利面，尤其是番茄酱的那种。'), 0.2671944797039032)]
[Document(metadata={'page': 1.0, 'account_id': None}, page_content='笨笨是一只很喜欢睡觉的猫咪'), Document(metadata={'page': 3.0, 'account_id': None}, page_content='猫咪在窗台上打盹，看起来非常可爱。'), Document(metadata={'page': 10.0, 'account_id': None}, page_content='我的狗喜欢追逐球，看起来非常开心。'), Document(metadata={'page': 7.0, 'account_id': None}, page_content='我的手机突然关机了，让我有些焦虑。')]
E:\code\llmops\llmops-api\venv\lib\site-packages\weaviate\warnings.py:303: ResourceWarning: Con004: The connection to Weaviate was not closed properly. This can lead to memory leaks.
            Please make sure to close the connection using `client.close()`.
  warnings.warn(
"""
